﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Strom.Client.Network;
using Strom.Core.Accounts;
using Strom.Core.Network;
using Strom.Core.Network.Packets;
using Strom.Core.Config;

namespace Strom.Client.GUI
{
    public partial class FormLogin : Form, Core.Graphic.ILoginForm
    {
        delegate void ShowFormCharacters(BaseClient client, List<Core.Characters.Character> characters);

        public FormLogin()
        {
            InitializeComponent();
            ClientManager.GUI = new GUIManager {Login = this};
            Loader.Load();
        }

        protected override void OnClosed(EventArgs e)
        {
            AutoConfig.SaveConfig("Config.xml");
            if (ClientManager.GUI.Game != null)
                ClientManager.GUI.Game.Dispose();
            base.OnClosed(e);
        }

        private void txtID_TextChanged(object sender, EventArgs e)
        {
            TryToEnableConnectButton();
        }

        private void txtPASS_TextChanged(object sender, EventArgs e)
        {
            TryToEnableConnectButton();
        }

        void TryToEnableConnectButton()
        {
            if (txtID.Text.Length > 3 & txtPASS.Text.Length > 3)
                button1.Enabled = true;
            else
                button1.Enabled = false;
        }

        private void label3_Click(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e) // register
        {
            if (ClientManager.GUI.Register == null)
                new FormRegister().ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e) // connect
        {
            button1.Enabled = false;
            button2.Enabled = false;
            txtID.Enabled = false;
            txtPASS.Enabled = false;
            var client = new BaseClient();
            Parser.RegisterNewClientOnParser(client);

            try
            {
                client.Connect(RemoteServerConfig.ServerIP, RemoteServerConfig.ServerPort);
            }
            catch
            {
                MessageBox.Show("Impossible de se connecter au serveur.", "Strøm", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                button1.Enabled = true;
                button2.Enabled = true;
                txtID.Enabled = true;
                txtPASS.Enabled = true;
            }

            if(client.IsConnected)
                new LoginRequestPacket().Pack(client, (byte)1, txtID.Text, txtPASS.Text);
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            ClientManager.GUI.Login = this;
        }

        public void RegisterSuccess(BaseClient client)
        {
            client.Disconnect();
            MessageBox.Show("Votre compte a été créé.", "Strøm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            ClientManager.GUI.Register.Close();
        }

        public void RegisterFailed(BaseClient client)
        {
            client.Disconnect();
            MessageBox.Show("Ce nom de compte existe déjà.", "Strøm", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public void ConnectSuccess(BaseClient client, List<Core.Characters.Character> characters)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new ShowFormCharacters(ConnectSuccess), new object[] {client, characters});
            }
            else
            {
                ClientManager.Client = new MainClient(client, new Account(new AccountInfos(txtID.Text, txtPASS.Text)));
                Hide();
                new FormCharactersSelection(characters).Show();
            }
        }

        public void ConnectFailed(BaseClient client)
        {
            client.Disconnect();
            MessageBox.Show("Connexion à ce compte impossible. Il est possible que vos identifiants soient incorrects ou que quelqu'un soit déjà connecté sur ce compte.", "Strøm", MessageBoxButtons.OK, MessageBoxIcon.Error);
            button1.Enabled = true;
            button2.Enabled = true;
            txtID.Enabled = true;
            txtPASS.Enabled = true;
        }
    }
}
